package com.nowcoder.util;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.util.DigestUtils;

import javax.swing.*;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @author by lvshh
 * @version 1.0
 * @date 2020/4/22 18:19
 */
public class CommunityUtil {

    // 生成随机的字符串
    public static String generateUUID(){
        return UUID.randomUUID().toString().replaceAll("-","");
    }

    //md5加密
    public static String md5(String key){
        if (StringUtils.isBlank(key)){
            return null;
        }
//        Spring自带的工具类，byte数组加密为16进制字符串
        return DigestUtils.md5DigestAsHex(key.getBytes());
    }

    public static String getJSONString(int code, String msg, Map<String,Object> map){
        JSONObject json = new JSONObject();
        json.put("code",code);
        json.put("msg",msg);
        if (map != null){
            for (String key : map.keySet()) {
                json.put(key,map.get(key));
            }
        }
        return json.toJSONString();

    }

    public static String getJSONString(int code, String msg){
        return getJSONString(code, msg,null);
    }

    public static String getJSONString(int code){
        return getJSONString(code, null,null);
    }

    public static void main(String[] args) {
        Map<String,Object> map = new HashMap<>();
        map.put("name","张三");
        map.put("age",25);
        System.out.println(getJSONString(0,"ok",map));
    }
}
